package com.dominator.controller.api;

import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.typewrap.Dtos;
import com.dominFramework.core.utils.JsonUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.service.*;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.exception.ApiException;
import com.dominator.utils.network.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
@CrossOrigin
@RestController
@RequestMapping("/api/v1/common")
@Slf4j
public class ApiCommonController {

    @Autowired
    HttpServletRequest request;

    @Autowired
    ApiCommonService apiCommonService;

    @Autowired
    ApiPointTicketService apiPointTicketService;

    @Autowired
    ApiWzlService apiWzlService;

    @Autowired
    ApiUserService apiUserService;

    @Autowired
    ApiBankService apiBankService;

    @Autowired
    ApiLotteryService apiLotteryService;

    @Autowired
    ApiEntranceTicketService apiEntranceTicketService;


    /**
     * 获取openid
     *
     * @param paramStr
     * @return
     */
    @PostMapping("/getWxUserOpenId")
    public ApiMessage getWxUserOpenid(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            msg = apiCommonService.getWxUserOpenId(JsonUtils.toDto(paramStr));
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }

    /**
     * 微信下单weixinCreateOrder
     *
     * @param paramStr
     * @return
     */
    @PostMapping("/weixinCreateOrder")
    public ApiMessage weixinCreateOrder(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiCommonService.weixinCreateOrder(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }

    /**
     * 微信回调
     *
     * @param
     * @return
     */
    @GetMapping("/wexinCallBack")
    public ApiMessage wexinCallBack() {
        ApiMessage msg;
        try {
            msg = apiCommonService.wexinCallBack(request);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }
    /**
     * 微信回调
     *
     * @param
     * @return
     */
    @PostMapping("/wexinCallBack")
    public ApiMessage wexinCallBackPost() {
        return wexinCallBack();
    }



    /**
     * 查询用户
     *
     * @param paramStr
     * @return
     */
    @PostMapping("/getUser")
    public ApiMessage getUser(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            msg = apiUserService.getUser(JsonUtils.toDto(paramStr));
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }


    /**
     * 上传图片
     *
     * @param uploadPicture
     * @return
     */
    @PostMapping("/uploadPicture")
    public ApiMessage uploadPicture(@RequestParam(value = "uploadPicture") MultipartFile uploadPicture) {
        ApiMessage msg;
        try {
            msg = apiCommonService.uploadPicture(uploadPicture);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 首页
     *
     * @param
     * @return
     */
    @GetMapping("/homepage")
    public ApiMessage homepage() {
        ApiMessage msg;
        try {
            msg = apiCommonService.homepage();
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 获取图像验证码
     *
     * @param
     * @return
     */
    @PostMapping("/getPicVerifyCode")
    public ApiMessage getPicVerifyCode() {
        ApiMessage msg;
        try {
            msg = apiCommonService.getPicVerifyCode();
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 批量发码
     *
     * @return
     */
    @PostMapping(value = "/batchCreate")
    public ApiMessage batchCreate(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiPointTicketService.batchCreate(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 单码核销
     *
     * @return
     */
    @PostMapping(value = "/verification")
    public ApiMessage verification(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiPointTicketService.verification(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 单码核销
     *
     * @return
     */
    @PostMapping(value = "/getWxConfig")
    public ApiMessage getWxConfig(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiCommonService.getWxConfig(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 兑换券列表
     *
     * @return
     */
    @PostMapping(value = "/getPointTicketList")
    public ApiMessage getPointTicketList(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiPointTicketService.getPointTicketList(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 彩票二维码查询
     *
     * @param
     * @return
     */
    @PostMapping(value = "/lotteryQrcodeQuery")
    public ApiMessage lotteryQrcodeQuery() {
        ApiMessage msg = new ApiMessage();
        try {
            msg = apiWzlService.lotteryQrcodeQuery();
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 获取彩票联合登录ID
     *
     * @param paramStr
     * @return
     */
    @PostMapping(value = "/getLotteryUnionId")
    public ApiMessage getUnionId(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiWzlService.getUnionId(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }

    /**
     * 彩票二维码发送
     *
     * @param paramStr
     * @return
     */
    @PostMapping(value = "/lotteryQrcodeSend")
    public ApiMessage lotteryQrcodeSend(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiWzlService.lotteryQrcodeSend(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 获取微信支持的银行卡列表
     *
     * @return
     */
    @GetMapping(value = "/getWechatBankList")
    public ApiMessage getWechatBankList() {
        ApiMessage msg = new ApiMessage();
        try {
            msg = apiCommonService.getWechatBankList();
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }


    /**
     * 添加银行卡
     *
     * @param paramStr
     * @return
     */
    @PostMapping("/addBank")
    public ApiMessage addBank(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            msg = apiBankService.addBank(JsonUtils.toDto(paramStr));
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }

    /**
     * 获取用户银行卡列表
     *
     * @param paramStr
     * @return
     */
    @PostMapping("/getUserBankList")
    public ApiMessage getUserBankList(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            msg = apiBankService.getUserBankList(JsonUtils.toDto(paramStr));
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }

    /**
     * 删除用户银行卡
     *
     * @param paramStr
     * @return
     */
    @PostMapping("/deleteBank")
    public ApiMessage deleteBank(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            msg = apiBankService.deleteBank(JsonUtils.toDto(paramStr));
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }


    /**
     * 上传图片
     *
     * @param orcPicture
     * @return
     */
    @PostMapping("/ocr")
    public ApiMessage ocr(@RequestParam(value = "orcPicture") MultipartFile orcPicture) {
        ApiMessage msg;
        try {
            msg = apiCommonService.ocrPicture(orcPicture);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }




    /**
     * 彩票返奖
     * @return
     */
    @PostMapping(value = "/betBonusMoney")
    public ApiMessage betBonusMoney(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiLotteryService.betBonusMoney(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 微信上传小票，传七牛
     *
     * @param
     * @return
     */
    @PostMapping(value = "/ocrWeixinTicket")
    public ApiMessage ocrWeixinTicket(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto= JsonUtils.toDto(paramStr);
            msg = apiCommonService.ocrWeixinTicket(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * orc识别url图片
     *
     * @param
     * @return
     */
    @PostMapping(value = "/ocrPictureByUrl")
    public ApiMessage ocrPictureByUrl(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto= JsonUtils.toDto(paramStr);
            msg = apiCommonService.ocrPictureByUrl(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 获取微信用户个人信息
     *
     * @param
     * @return
     */
    @PostMapping(value = "/getUserInfoByOpenId")
    public ApiMessage getUserInfoByOpenId(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto= JsonUtils.toDto(paramStr);
            msg = apiCommonService.getUserInfoByOpenId(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }
}
